package com.zisu.wcsopcua.opcua.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @Author: 蔡翔
 * @Date: 2019/10/12 15:15
 * @Version 1.0
 */
@RestController
@RequestMapping("/testPLC2")
public class WmcController2plc2 {
/*
    private static final Logger logger = LoggerFactory.getLogger(WmcController2plc2.class);
    @Autowired
    UAService uaService;

    *//**
     * 测试real 类型（get / set）
     **//*
    @PostMapping("/noRR")
    public R noRR() throws Exception {
        return R.ok().put("OK",123);
    }

    @GetMapping("/removeThisPlc")
    public R removeThisPlc() throws Exception {
        return R.ok().put("result",uaService.dynamicRemovePlc("plc2"));
    }

    @GetMapping("testWriteOneVar")
    public R testWriteOneVar(Integer a) throws Exception {
        System.out.println("uaService.hashCode(): "+uaService.hashCode());

        Boolean aBoolean = uaService.setValue(PLCVar.INT32_3, a,"plc2");
        return R.ok().put("result",aBoolean);
    }

    @GetMapping("testWriteTwoVar")
    public R testWriteTwoVar(Boolean b,Integer a) throws Exception {
        System.out.println("uaService.hashCode(): "+uaService.hashCode());

        List<PLCVar> list = new ArrayList<>();
        list.add(PLCVar.INT32_3);
        list.add(PLCVar.Boolean_3);

        List<Object> value = new ArrayList<>();
        value.add(a);
        value.add(b);

        Boolean aBoolean = uaService.setValues(list, value,"plc2");
        return R.ok().put("result",aBoolean);

    }

    @GetMapping("testReadOneVar")
    public R testReadOneVar() throws Exception {
        System.out.println("uaService.hashCode(): "+uaService.hashCode());

        DataValue dataValue = uaService.getValue(PLCVar.INT32_3,"plc2");
        return R.ok().put("result",dataValue.getValue().getValue());
    }

    @GetMapping("testReadTwoVar")
    public R testReadTwoVar() throws Exception {
        System.out.println("uaService.hashCode(): "+uaService.hashCode());

        List<PLCVar> list = new ArrayList<>();
        list.add(PLCVar.Boolean_3);
        list.add(PLCVar.INT32_3);

        List<DataValue> values = uaService.getValues(list ,"plc2");
        List<Object> res = new ArrayList<>();
        for(DataValue dv:values){
            res.add(dv.getValue().getValue());
        }
        return R.ok().put("result",res);
    }

    @GetMapping("testReadBooleanArray")
    public R testReadBooleanArray() throws Exception {
        System.out.println("uaService.hashCode(): "+uaService.hashCode());
        DataValue aBoolean = null;
        try {
            aBoolean = uaService.getValue(PLCVar.BooleanArray_3,"plc2");
        }catch (Exception e){

            System.out.println("err status: "+uaService.extractError(e.getMessage()));
        }
        return R.ok().put("result",aBoolean.getValue().getValue());
    }

    @GetMapping("subscribeInt32")
    public R subscribeInt32() throws Exception {
        uaService.subscribe(PLCVar.INT32_3,new Double(1000),(item,dataValue)->{

            System.err.println("（测试是否 每隔intervel 都会执行这个回调方法） subscription value received: item:NodeId : " +
                    ""+item.getReadValueId().getNodeId()+", value :"+ dataValue.getValue());
            System.out.println();
            if(dataValue.getValue().getValue().equals(11)){
                System.err.println("subscribeInt32  I am over");
            }
        },"plc2",1);
        return R.ok().put("result","ok");
    }

    @GetMapping("subscribeMultiply")
    public R subscribeMultiply() throws Exception {
        List<PLCVar> list = new ArrayList<>();
        list.add(PLCVar.Boolean_3);
        list.add(PLCVar.INT32_3);

        uaService.subscribeValues(list,new Double(1000),(item,dataValue)->{

            System.err.println("（测试是否 每隔intervel 都会执行这个回调方法） subscription value received: item:NodeId : " +
                    ""+item.getReadValueId().getNodeId()+", value :"+ dataValue.getValue());
            System.out.println();
            if(dataValue.getValue().getValue().equals(11)){
                System.err.println("subscribeInt32  I am over");
            }
        },"plc2");
        return R.ok().put("result","ok");
    }

    @GetMapping("subscribeBoolen")
    public R subscribeBoolen() throws Exception {
        uaService.subscribe(PLCVar.Boolean_3,new Double(1000),(item,dataValue)->{

            System.err.println("（测试是否 每隔intervel 都会执行这个回调方法） subscription value received: item:NodeId : " +
                    ""+item.getReadValueId().getNodeId()+", value :"+ dataValue.getValue());
            System.out.println();
            if(dataValue.getValue().getValue().equals(false)){
                System.err.println("subscribeBoolen  I am over");
            }
        },"plc2",1);
        return R.ok().put("result","ok");
    }
    */

}
